package com.crm.service.purchaseService;

import com.crm.dao.productionDAO.ProductionDetilsDAO;
import com.crm.dao.purchaseDAO.PurchaseproductiondetailDAO;
import com.crm.pojo.ProductionDetail;
import com.crm.pojo.Purchaseproductiondetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class PurproductDetailsService {
    @Autowired
    private PurchaseproductiondetailDAO purchaseproductiondetailDAO;
    @Autowired
    private ProductionDetilsDAO productionDetilsDAO;

    //逻辑批量删除根据采购id
    public void deleteAllBypurId(Long purId){
        if(!purchaseproductiondetailDAO.findAllByPid(purId).isEmpty()) {
            List<Purchaseproductiondetail> purchaseproductiondetails = purchaseproductiondetailDAO.findAllByPid(purId);
            for (Purchaseproductiondetail purchaseproductiondetail : purchaseproductiondetails) {
                purchaseproductiondetail.setPurchaseproductiondetailIsDel("已删除");
                purchaseproductiondetailDAO.save(purchaseproductiondetail);
            }
        }
    }
    //查找采购总价值
    public Double findAllByPurid(Long purid){
        Double purMoney = 0d;
        List<Purchaseproductiondetail> purchaseproductiondetails = purchaseproductiondetailDAO.findAllByPid(purid);
        for (Purchaseproductiondetail p:purchaseproductiondetails) {
            if(p.getPpcost()==null){
                p.setPpcost(0d);
            }
            purMoney+=p.getPpcost();
        }
        return purMoney;
    }
    //查找某采购单下的采购产品明细(包含部分产品信息) 接口
    public List<Purchaseproductiondetail> findAllppdByPurid(Long purid){
        if(!purchaseproductiondetailDAO.findAllByPid(purid).isEmpty()){
            List<Purchaseproductiondetail> purchaseproductiondetails= purchaseproductiondetailDAO.findAllByPid(purid);
            for (Purchaseproductiondetail p:purchaseproductiondetails) {
                ProductionDetail pro = productionDetilsDAO.findOne(p.getProid());
                p.setPimg(pro.getPimg());
                p.setPcompany(pro.getPcompany());
                p.setPname(pro.getPname());
                p.setpCategoryname(pro.getpCategoryname());
            }
            return purchaseproductiondetails;
        }else{
            return null;
        }
    }

    //新增采购产品明细
    public void addNewdetails(Purchaseproductiondetail purchaseproductiondetail){
        purchaseproductiondetail.setPurchaseproductiondetailIsDel("否");
        purchaseproductiondetailDAO.save(purchaseproductiondetail);
    }
    //根据采购产品明细id查询
    public Purchaseproductiondetail findOne(Long ppdid){
        return purchaseproductiondetailDAO.findOne(ppdid);
    }
    //根据采购产品明细id删除
    public Purchaseproductiondetail deleteOne(Purchaseproductiondetail p){
        p.setPurchaseproductiondetailIsDel("是");
        return purchaseproductiondetailDAO.save(p);
    }
    //根据采购产品明细id设置为待退货
    public Purchaseproductiondetail setrg(Purchaseproductiondetail p){
        p.setPurchaseproductiondetailIsDel("待退货");
        return purchaseproductiondetailDAO.save(p);
    }
    //设置为已退货
    public Purchaseproductiondetail setrg2(Purchaseproductiondetail p){
        p.setPurchaseproductiondetailIsDel("已退货");
        return purchaseproductiondetailDAO.save(p);
    }

}
